package com.ruileroi.ai.controller;

import com.ruileroi.ai.repository.ChatHistoryRepository;
import com.ruileroi.ai.service.ISpringAiChatRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

//import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

/**
 * @program: ruileroi-ai
 * @description:
 * @author: wangrui
 * @create: 2025-04-27 21:49
 **/

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CustomerServiceController {

    private final ChatClient serviceChatClient;

//    private final ChatHistoryRepository redisChatHistory;
    private final ChatHistoryRepository inMemoryChatHistoryRepository;
    private final ISpringAiChatRecordService recordService;

    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> service(String prompt, String chatId) {
        // 1.保存会话id
//        inMemoryChatHistoryRepository.save("service", chatId);
        recordService.saveRecord("service", chatId);
        // 2.请求模型
        return serviceChatClient.prompt()
                .user(prompt)
//                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream()
                .content();
    }
}
